﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Description;
using WLYT.Admin.WebApi.Component;
using WLYT.Admin.WebApi.Models;
using WLYT.Application.Interfaces;
using WLYT.Domain.Model;

namespace WLYT.Admin.WebApi.Controllers
{
    [System.Web.Http.Description.ApiExplorerSettings(IgnoreApi = true)]
    public class UserController : BaseController
    {
        private readonly IUserService UserService;

        public UserController(IUserService userService)
        {
            UserService = userService;
        }

        [HttpGet]
        [Route("api/User/GetData")]
        public async Task<Table<User>> GetData(string search, int pageSize, int pageIndex)
        {
            var response = await UserService.GetModelsByPageAsync(search, pageSize, pageIndex);
            return new Table<User>(response.Item1, response.Item2);
        }

        [HttpGet]
        [Route("api/User/GetModel")]
        public Task<User> GetModel(int id)
        {
            return UserService.GetByIdAsync(id);
        }

        [HttpPost]
        [Route("api/User/Save")]
        public Task<bool> Save(UserRequest request)
        {
            return UserService.Save(request.Model);
        }

        [HttpPost]
        [Route("api/User/EditPassword")]
        public Task<bool> EditPassword(UserRequest request)
        {
            return UserService.EditPassword(CurrentUser.Id, request.Password, request.NewPassword);
        }

        [Route("api/User/Remove")]
        public Task<bool> Remove(int id)
        {
            return UserService.DeleteAsync(id);
        }
    }
}